package com.ruoyi.oa.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.oa.domain.OaHoliday;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaHolidayVo;
import com.ruoyi.oa.domain.bo.OaHolidayBo;
import com.ruoyi.oa.service.IOaHolidayService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 假期
 *
 * @author ruoyi
 * @date 2023-07-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/holidayCollect")
public class OaHolidayController extends BaseController {

    private final IOaHolidayService iOaHolidayService;

    /**
     * 查询假期列表
     */
    @SaCheckPermission("oa:holidayCollect:list")
    @GetMapping("/list")
    public TableDataInfo<OaHolidayVo> list(OaHolidayBo bo, PageQuery pageQuery) {
        return iOaHolidayService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出假期列表
     */
    @SaCheckPermission("oa:holidayCollect:export")
    @Log(title = "假期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaHolidayBo bo, HttpServletResponse response) {
        List<OaHolidayVo> list = iOaHolidayService.queryList(bo);
        ExcelUtil.exportExcel(list, "假期", OaHolidayVo.class, response);
    }

    /**
     * 获取假期详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("oa:holidayCollect:query")
    @GetMapping("/{id}")
    public R<OaHolidayVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaHolidayService.queryById(id));
    }

    /**
     * 新增假期
     */
    @SaCheckPermission("oa:holidayCollect:add")
    @Log(title = "假期", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaHolidayBo bo) {
        return toAjax(iOaHolidayService.insertByBo(bo));
    }

    /**
     * 修改假期
     */
    @SaCheckPermission("oa:holidayCollect:edit")
    @Log(title = "假期", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaHolidayBo bo) {
        return toAjax(iOaHolidayService.updateByBo(bo));
    }

    /**
     * 删除假期
     *
     * @param ids 主键串
     */
    @SaCheckPermission("oa:holidayCollect:remove")
    @Log(title = "假期", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaHolidayService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/collectData")
    @SaIgnore
    public void collectData() throws ParseException {
        Calendar now = Calendar.getInstance();
        int i = now.get(Calendar.YEAR);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = DateUtils.addHolidayAndWeekends(i + "");
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            OaHolidayBo oaHoliday = new OaHolidayBo();
            String key = entry.getKey();
            Date date = dateFormat.parse(key);
            Map<String,Object> mapValue=(Map<String, Object>)entry.getValue();
            String name = mapValue.get("name").toString();
            String type = mapValue.get("type").toString();
            oaHoliday.setHolidayDate(date);
            oaHoliday.setName(name);
            oaHoliday.setType(type);
            iOaHolidayService.insertByBo(oaHoliday);
        }
    }
}
